import {defineStore} from "pinia";
import router from "../route";
import {type} from "../model/type";

export const useWindowsStore = defineStore('windows', {
    state: () => {
        return {
            // 菜单右键选中的窗口标签索引值
            select:0,
            // index 当前选中的窗口标签在 windowLabels 中的索引值
            index: parseInt(sessionStorage.getItem('window-index') as string) as number || 0 as number,
            windowLabels: JSON.parse(sessionStorage.getItem('window-arr') as string) as type.WindowLabel[] || [] as type.WindowLabel[]
        }
    },
    getters: {},
    actions: {
        UpdateSelect(index:number){
            this.select=index
        },
        UpdateWindow(value: type.WindowLabel) {
            if (value.path === '/index/home') {
                return;
            }
            let arr = this.windowLabels
            let index = this.index
            if (arr.length === 0) {
                arr.push(value)
                this.update(arr, 0)
                return
            }
            // 取消原来的选中
            arr[index].check = false
            let number = -1
            for (let i = 0; i < arr.length; i++) {
                if (arr[i].path === value.path) {
                    number = i
                    break
                }
            }
            if (number === -1) {
                arr.push(value)
                // 新添加窗口，默认选中最后一个
                this.update(arr, arr.length - 1)
            } else {
                arr[number].check = true
                this.update(arr, number)
            }
        },
        openWindow(path: string, value: number) {
            if (path === '/index/home') {
                return;
            }
            let arr = this.windowLabels
            for (let i = 0; i < arr.length; i++) {
                arr[i].check = false
            }
            arr[value].check = true
            this.update(arr, value)
        },
        closeWindow(check: boolean, value: number) {
            let arr = this.windowLabels
            let index = this.index
            let number = value

            // 判断当前窗口是否处于打开状态，打开状态需要切换到后面一个窗口
            if (check) {
                // 删除最后一个窗口
                if (number === arr.length - 1) {
                    if (number === 0) {
                        router.push('/index/home')
                        arr = []
                        index = 0
                        this.update(arr, index)
                        return
                    }
                    // 打开前面一个窗口
                    arr[number - 1].check = true
                    index = number - 1
                    router.push(arr[index].path)
                } else {
                    arr[number + 1].check = true
                    index = number
                    router.push(arr[number + 1].path)
                }
            }
            arr.splice(number, 1)
            this.update(arr, index)
        },

        //  关闭指定标签
        closeLabel(){
            // 获取选中窗口状态
            let labels:type.WindowLabel[] = this.windowLabels
            let label = labels[this.select]
            if (label.check){
                // 如果当前选中的窗口是打开状态 需要调用 closeWindow
                this.closeWindow(label.check,this.select)
            }else {
                // 直接移除标签
                labels.splice(this.select,1)
                //  移除一个未打开的标签不需要更新 index
                this.update(labels,this.index)
            }
        },
        // 关闭指定标签之外的所有标签
        closeOther(){
            let labels:type.WindowLabel[] = this.windowLabels
            let label = labels[this.select]
            label.check=true
            router.push(label.path)
            let arr :type.WindowLabel[] =[label]
            this.update(arr,0)
        },
        // 关闭当前所有标签
        closeAll(){
            let arr :type.WindowLabel[] =[]
            this.update(arr,0)
            router.push("/index/home")
        },
        update(arr: type.WindowLabel[], index: number) {
            this.windowLabels = arr
            this.index = index
            sessionStorage.setItem("window-arr", JSON.stringify(this.windowLabels))
            sessionStorage.setItem("window-index", this.index.toString())
        },

        // 更新 用户组织或者角色调用 更新当前的窗口信息,更具路径判断
        updateOrgAndRole(){
            // 查询当前用户 组织及其角色对应的 路由权限

        }
    }
})